package org.zjx.service;

import org.zjx.common.entity.Address;
import org.zjx.common.entity.Inventor;
import org.zjx.common.entity.Pet;
import org.zjx.common.entity.Quest;
import org.zjx.common.entity.state.PetAttributes;
import org.zjx.common.entity.state.RoleAttributes;
import org.zjx.common.bean.Role;
import org.zjx.common.service.UpdateStateService;
import org.zjx.db.RoleDB;
import org.zjx.server.annotation.RpcService;
import org.zjx.state.*;

import java.util.ArrayList;
import java.util.List;

@RpcService(UpdateStateService.class)
public class UpdateStateServiceImpl implements UpdateStateService {
    @Override
    public boolean updatePlayer(String playerId, RoleAttributes roleAttributes) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        Role role = new RoleDB().findByRoleId(playerId);
        if (playerState == null || role == null || roleAttributes == null) return false;

        if (roleAttributes.getLevel() > 0) {
            role.setLevel(roleAttributes.getLevel());
        }

        if (roleAttributes.getPotential() > 0) {
            role.setPotential(roleAttributes.getPotential());
        }

        if (roleAttributes.getGangValue() > 0){
            role.setGangValue(roleAttributes.getGangValue());
        }

        if (roleAttributes.getMaxHp() > 0){
            role.setMaxHp(roleAttributes.getMaxHp());
        }

        if (roleAttributes.getHp() >= 0){
            playerState.setHp(roleAttributes.getHp());
        }

        if (roleAttributes.getInjury() >= 0){
            playerState.setInjury(roleAttributes.getInjury());
        }

        if (roleAttributes.getMaxMp() > 0){
            role.setMaxMp(roleAttributes.getMaxMp());
        }

        if (roleAttributes.getMp() >= 0){
            playerState.setMp(roleAttributes.getMp());
        }

        if (roleAttributes.getMaxRage() > 0){
            role.setRage(roleAttributes.getMaxRage());
        }

        if (roleAttributes.getRage() >= 0){
            playerState.setRage(roleAttributes.getRage());
        }

        if (roleAttributes.getMaxVitality() > 0){
            role.setVitality(roleAttributes.getMaxVitality());
        }

        if (roleAttributes.getVitality() >= 0){
            playerState.setVitality(roleAttributes.getVitality());
        }

        if (roleAttributes.getMaxPhysical() > 0){
            role.setPhysical(roleAttributes.getMaxPhysical());
        }

        if (roleAttributes.getPhysical() >= 0){
            playerState.setPhysical(roleAttributes.getPhysical());
        }

        if (roleAttributes.getPotential() >= 0){
            role.setPotential(roleAttributes.getPotential());
        }

        if (roleAttributes.getExperience() >= 0){
            role.setExperience(roleAttributes.getExperience());
        }

        return false;
    }

    @Override
    public boolean updateAddress(String playerId, Address address) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null || address == null) return false;

        Address before = playerState.getAddress();

        if (!address.equals(before)) {
            playerState.setAddress(address);
            playerState.setStandStartTime(System.currentTimeMillis());
        }

        return true;
    }

    @Override
    public boolean updatePet(String playerId, PetAttributes petAttributes) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        Role role = new RoleDB().findByRoleId(playerId);
        if (playerState == null || role == null || petAttributes == null) return false;
        PetState petState = playerState.getPetState();
        if (petState == null){
            petState = new PetState();
            playerState.setPetState(petState);
        }

        if (petAttributes.getMaxNumber() >= 3){
            role.setMaxPetValue(petAttributes.getMaxNumber());
            petState.setMaxPetNumber(petAttributes.getMaxNumber());
        }

        if (petAttributes.getPetNumber() >= 0){
            petState.setPetNumber(petAttributes.getPetNumber());

            if (petState.getPets() == null) {
                petState.setPets(new ArrayList<>());
            }
            while (petState.getPets().size() < petAttributes.getPetNumber()){
                petState.getPets().add(null);
            }

        }

        Pet pet = getPet(petAttributes);

        petState.getPets().add(petAttributes.getIndex(),pet);

        return true;
    }

    @Override
    public boolean updateInventor(String playerId, int barSort, List<Inventor> inventors) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null || inventors == null) return false;
        InventorState inventorState = playerState.getInventorState();
        if (inventorState == null){
            inventorState = new InventorState();
            playerState.setInventorState(inventorState);
        }

        if (barSort == 0){
            inventorState.setSlot(inventors);
        }else if (barSort == 1){
            inventorState.setBag(inventors);
        }else if (barSort == 2){
            inventorState.setTaskbar(inventors);
        }else if (barSort == 3){
            inventorState.setEquipmentBar(inventors);
        }else if (barSort == 4){
            inventorState.setJewelryBar(inventors);
        }else if (barSort == 5){
            inventorState.setTalismanBar(inventors);
        }

        return true;
    }

    @Override
    public boolean updateQuestAttributes(String playerId, List<Quest> quests) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null || quests == null) return false;
        QuestState questState = playerState.getQuestState();
        if (questState == null){
            questState = new QuestState();
            playerState.setQuestState(questState);
        }
        playerState.getQuestState().setQuests(quests);
        return true;
    }

    @Override
    public boolean updateBattle(String playerId, boolean battle) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null) return false;
        playerState.setBattle(battle);
        return true;
    }

    @Override
    public boolean updateTeam(String playerId, String teamId, int sort, boolean leader, boolean inTeam) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null) return false;
        TeamState teamState = new TeamState();
        teamState.setTeamId(teamId);
        teamState.setSort(sort);
        teamState.setLeader(leader);
        teamState.setInTeam(inTeam);
        playerState.setTeamState(teamState);
        return true;
    }

    private static Pet getPet(PetAttributes petAttributes) {
        Pet pet = new Pet();
        pet.setName(petAttributes.getName());
        pet.setLevel(petAttributes.getLevel());
        pet.setFighting(petAttributes.isFighting());
        pet.setMaxHp(petAttributes.getMaxHp());
        pet.setHp(petAttributes.getHp());
        pet.setMaxMp(petAttributes.getMaxMp());
        pet.setMp(petAttributes.getMp());
        pet.setPotential(petAttributes.getPotential());
        pet.setExperience(petAttributes.getExperience());
        pet.setSkills(petAttributes.getSkills());
        return pet;
    }

}
